export function getBasename(basename, pathname) {
  if (!Array.isArray(basename)) {
    return basename
  }
  const basenames = basename.filter(value => pathname.indexOf(value) === 0)
  if (!basenames[0]) {
    return ''
  }
  return basenames[0]
}

export function shareRoot(rootPath) {
  if (rootPath.charAt(rootPath.length - 1) === '/') {
    rootPath = rootPath.substr(0, rootPath.length - 1)
  }
  const ROOT_RE = new RegExp('^' + rootPath)
  return function(req, res, next) {
    if (ROOT_RE.test(req.url)) {
      req.url = req.url.replace(ROOT_RE, '')
      req.basename = rootPath
      if (req.url === '') {
        req.url = '/'
      }
    }
    next()
  }
}